<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="负责人：">
          <el-select v-model="formInline.principal" filterable clearable placeholder="请输入关键词搜索">
            <el-option v-for="(item,index) in userList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户信息：">
          <el-input v-model="formInline.customerTerm" placeholder="姓名/电话"></el-input>
        </el-form-item>
        <el-form-item label="处理状态：">
          <el-select v-model="formInline.status" clearable>
            <el-option label="全部" value=""></el-option>
            <el-option label="已处理" :value="1"></el-option>
            <el-option label="未处理" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间筛选：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" unlink-panels :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="onSearch">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <el-table ref="elTable" class="el_table" size="small" :data="tableData">
        <el-table-column prop="settleRegisterCnt" label="客户信息">
          <template slot-scope="{row}">
            <tab-user :user="showFormatter(row)"></tab-user>
          </template>
        </el-table-column>
        <el-table-column prop="notifyContent" label="待办内容" show-overflow-tooltip />
        <el-table-column prop="notifyTime" label="通知时间" show-overflow-tooltip>
          <template slot-scope="{row}">{{showTime(row.notifyTime)}}</template>
        </el-table-column>
        <el-table-column prop="statusDesc" label="处理状态" show-overflow-tooltip />
        <el-table-column prop="principal" label="负责人" show-overflow-tooltip />
        <el-table-column prop="settleRegisterCnt" label="操作" align="center" width="120">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" :underline="false" @click="linkEvent(row)" v-if="row.status != 1">处理</el-link>
            <el-link type="primary" class="el_link" :underline="false" @click="getInfo(row)">修改</el-link>
          </template>
        </el-table-column>

      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 修改 开始 -->
    <glsx-dialog :show="showDialog" title="修改" @close="closeDialog()">
      <div>
        <el-form ref="editForm" :model="editForm" :rules="editFormRules" label-width="80px">
          <el-form-item label="提醒时间：" prop="notifyTime">
            <el-date-picker v-model="editForm.notifyTime" type="datetime" value-format="yyyy-MM-dd hh:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="提醒内容：" prop="notifyContent">
            <el-input type="textarea" v-model="editForm.notifyContent"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="" slot="footer">
        <el-button type="primary" size="small" :loading="aloading" @click="saveInfo()">保存</el-button>
        <el-button type="default" size="small" @click="closeDialog()">取消</el-button>
      </div>
    </glsx-dialog>
    <!-- 修改 end -->

  </div>
</template>

<script>
import {
  GETTODOSEARCH,
  UPDATETODOHANDLE,
  GETUSEROPTIONS,
  GETTODOINFO,
  EDITTODOINFO,
} from "@/http/system";
import { showToast, showModal, deepClone } from "@/common/util";
import GlsxDialog from "@/components/Dialog/index";
import TabUser from "@/components/user";
import moment from "moment";
import { systemToDoShortcuts } from "@/common/date";
export default {
  name: "BookPage",
  props: {
    status: {
      type: [String, Number],
      default: "",
    },
  },
  components: {
    GlsxDialog,
    TabUser,
  },
  data() {
    return {
      formInline: { status: 0 },
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      userList: [],
      showDialog: false,
      editForm: {},
      editFormRules: {
        notifyTime: [
          { required: true, message: "请输入提醒时间", trigger: "change" },
        ],
        notifyContent: [
          { required: true, message: "请输入提醒内容", trigger: "blur" },
        ],
      },
      aloading: false,
      pickerOptions: {
        shortcuts: systemToDoShortcuts,
      },
    };
  },
  mounted() {
    this.getList();
    this.getUser();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = { status: 0 };
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = JSON.parse(JSON.stringify(this.formInline));
      let n = oParams.orderTime;

      if (n && n.length > 0) {
        oParams.sDate = n[0];
        oParams.eDate = n[1];
      }
      delete oParams.orderTime;
      oParams.pageNumber = this.pageNumber;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GETTODOSEARCH(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    //处理通知
    linkEvent(row) {
      let _this = this;
      showModal({
        message: "确认处理该通知吗？",
        type: "warning",
        showCancelButton: true,
        success: function (e) {
          // if (e.confirm) {
          console.log("66666666");
          UPDATETODOHANDLE({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "处理成功",
                type: "success",
              });
              _this.getList();
            }
          });
          // }
        },
      });
    },

    // 获取负责人列表
    getUser(name) {
      GETUSEROPTIONS({ username: name }).then((res) => {
        this.userList = res.data || [];
      });
    },

    // 格式化用户数据
    showFormatter(option) {
      let row = deepClone(option);
      if (!row) {
        return {};
      }
      row.id = row.clueId;
      row.name = row.customerName;
      row.phone = row.customerPhone;
      return row;
    },

    // 格式化展示时间
    showTime(str) {
      if (!str) {
        return "";
      }
      return moment(str).format("yyyy-MM-DD HH:mm");
    },

    // 获取代办通知详情
    getInfo(row) {
      let oParams = {};
      oParams.id = row.id;
      this.showDialog = true;
      let o = {};
      o.id = row.id;
      o.notifyTime = row.notifyTime;
      o.notifyContent = row.notifyContent;
      this.editForm = o;
      //GETTODOINFO(oParams).then((res) => {});
    },

    // 关闭弹框
    closeDialog() {
      this.showDialog = false;
      this.editForm = {};
    },

    // 保存数据
    saveInfo() {
      let _this = this;
      _this.$refs.editForm.validate((valid) => {
        if (!valid) {
          return false;
        }
        let oParams = deepClone(_this.editForm);
        _this.aloading = true;
        EDITTODOINFO(oParams)
          .then((res) => {
            _this.aloading = false;
            if (res.code == 200) {
              showToast({
                message: "修改成功",
                type: "success",
              });
              _this.getList();
              _this.closeDialog();
            }
          })
          .catch((error) => {
            _this.aloading = false;
          });
      });
    },

    // 搜索
    onSearch(){
      this.pageNumber = 1;
      this.getList()
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
.el_link {
  margin: 0 4px;
}
</style>
